﻿using Sail.FileSystems.VirtualPath;

namespace Sail.Utility.Extensions
{
    public static class VirtualPathProviderExtensions
    {
        public static string GetProjectReferenceVirtualPath(this IVirtualPathProvider virtualPathProvider,
                                                            string projectPath, string referenceName, string hintPath)
        {
            string basePath = virtualPathProvider.GetDirectoryName(projectPath);
            string virtualPath = virtualPathProvider.GetReferenceVirtualPath(basePath, referenceName, hintPath);

            if (!string.IsNullOrEmpty(virtualPath))
            {
                return virtualPathProvider.Combine(basePath, virtualPath);
            }

            return null;
        }

        public static string GetReferenceVirtualPath(this IVirtualPathProvider virtualPathProvider, string basePath,
                                                     string referenceName, string hintPath)
        {
            if (!string.IsNullOrEmpty(hintPath))
            {
                if (virtualPathProvider.TryFileExists(virtualPathProvider.Combine(basePath, hintPath)))
                    return hintPath;
            }

            string relativePath = virtualPathProvider.Combine("bin", referenceName + ".dll");
            if (virtualPathProvider.TryFileExists(virtualPathProvider.Combine(basePath, relativePath)))
                return relativePath;

            return null;
        }
    }
}